﻿using PEProtocol;

/**
 * 副本业务系统
 */
public class FuBenSys{
    private static FuBenSys instance = null;

    public static FuBenSys Instance{
        get{
            if (instance == null){
                instance = new FuBenSys();
            }

            return instance;
        }
    }

    private CacheSvc cacheSvc;

    private CfgSvc cfgSvc;

    public void Init(){
        cacheSvc = CacheSvc.Instance;
        cfgSvc = CfgSvc.Instance;
        PECommon.Log("FuBenSys Init Done");
    }

    public void ReqFBFight(MsgPack pkt){
        ReqBFFight data = pkt.msg.reqBfFight;

        GameMsg msg = new GameMsg{
            cmd = (int)CMD.ResFBFight
        };

        PlayerData pd = cacheSvc.GetPlayerDataBySession(pkt.session);
        int power = cfgSvc.GetMapCfg(data.fbid).power;
        if (pd.fuben < data.fbid){
            msg.err = (int) ErrorCode.ClientDataError;
        }
        else if(pd.power < power){
            msg.err = (int) ErrorCode.LackPower;
        }
        else{
            pd.power -= power;
            if (cacheSvc.UpdatePlayerData(pd.id, pd)){
                ResBFFight resBfFight = new ResBFFight{
                    fbid = data.fbid,
                    power = pd.power
                };
                msg.resBfFight = resBfFight;
            }
            else{
                msg.err = (int) ErrorCode.UpdateDBError;
            }
        }
        
        pkt.session.SendMsg(msg);
    }
}